<template>
    <div class="app-container">
        <el-form ref="patDischargeFormRef" :model="form" :rules="rules" label-width="100px">
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="床号：" prop="bedNo">
                        <span>{{ form.bedNo }}</span>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="姓名：" prop="name">
                        <span>{{ form.name }}</span>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="性别：" prop="sex">
                        <span><svg-icon :icon-class="dictStore.getMateDictInfo(
                                        dictDataMap['sys_user_sex'], form.sex).dictValue === '1'
                                        ? 'man'
                                        : 'woman'
                                        " style="margin-left: 2px"/></span>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="预交金：" prop="patPrepaymentSum">
                        <span class="ys-css">{{ form.patPrepaymentSum }}</span>元
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="诊断：" prop="diagnosisStr">
                        <span>{{ form.diagnosisStr }}</span>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="出院科室：" prop="deptStayedName">
                        <span>{{ form.deptStayedName }}</span>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="出院病区：" prop="wardCodeName">
                        <span>{{ form.wardCodeName }}</span>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="出院时间：" prop="patDischargeTime">
                        <span>{{ form.patDischargeTime }}</span>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-form-item label="出院方式：" prop="dischargeMethod">
                <el-select v-model="form.dischargeMethod" placeholder="请选择出院方式">
                    <el-option
                        v-for="dict in dictDataMap['discharge_disposition_dict']"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    ></el-option>
                </el-select>
            </el-form-item>
        </el-form>
    </div>
</template>

<script lang="ts" setup>
import {delPatDischarge, listPatDischarge, savePatDischarge} from "@/api/patDischarge/patDischarge.ts";
import {useUserStoreHook} from "@/store/modules/user.ts"
import {useDictStoreHook} from "@/store/modules/dict.ts";

const dictStore = useDictStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData('discharge_disposition_dict', 'sys_user_sex'))
const useUserStore = useUserStoreHook()
const emits = defineEmits(["saveOk"])
const {proxy} = getCurrentInstance() as any;
const props = defineProps({
    visitId: {
        type: String,
        default: ""
    },
    patientId: {
        type: String,
        default: ""
    }
})
const patDischargeFormRef = ref();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
const form = ref<any>({})
const rules = ref({
    dischargeMethod: [
        {
            required: true, message: "出院方式不能为空", trigger: "change"
        }
    ]
})

/** 提交按钮 */
function savePatDischargeFun() {
    if (!form.value.dischargeMethod) {
        return ElMessage.error("未开出院医嘱、出院通知单，不能提交。请联系医生开具后再操作。")
    }
    (patDischargeFormRef.value as any).validate(valid => {
        if (valid) {
            ElMessageBox.confirm('是否确认要出院',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            ).then(() => {
                const data = {
                    visitId: form.value.visitId,
                    patientId: form.value.patientId,
                    dischargeMethod: form.value.dischargeMethod,
                    patDischargeTime: form.value.patDischargeTime,
                    orgId: userInfo.orgId,
                }
                savePatDischarge(data).then(() => {
                    emits("saveOk")
                    ElMessage.success("出院成功");
                }).finally(() => {
                });
            }).catch(() => {
            })
        }
    });
}


/** 删除按钮操作 */
function handleDelete(row) {
    ElMessageBox.confirm('是否确认取消出院？',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        const patDischargeId = row.patDischargeId
        delPatDischarge(patDischargeId).then(() => {
            // emits("saveOk")
            form.value.patDischargeId = null;
            ElMessage.success("删除成功");
        })
    })
}

function initOpen(patientItem) {
    form.value = patientItem
    listPatDischarge({visitId: patientItem.visitId}).then((res: resType) => {
        if (res.data) {
            form.value.patDischargeTime = res.data[0].patDischargeTime
            form.value.dischargeMethod = res.data[0].dischargeMethod
        }
    })
}

/**
 * 清空
 */
function empty() {
    form.value.patDischargeTime = ''
    form.value.dischargeMethod = ''
    form.value.bedNo = ''
    form.value.name = ''
    form.value.sex = ''
    form.value.patPrepaymentSum = ''
    form.value.diagnosisStr = ''
    form.value.deptStayedName = ''
    form.value.wardCodeName = ''
    form.value.patDischargeTime = ''
    form.value.dischargeMethod = ''
}

defineExpose({
    initOpen,
    savePatDischargeFun,
    empty
})
</script>

<style scoped>

</style>
